package com.spzx.order.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.order.domain.dto.OrderSubmitDTO;
import com.spzx.order.domain.vo.OrderTradeVO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.service.IOrderInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author atguigu
 * @date 2024-12-25
 */
@Tag(name = "订单接口管理")
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController
{
    @Resource
    private IOrderInfoService orderInfoService;
    @InnerAuth
    @Operation(summary = "确认支付 根据订单号获取订单详情")
    @GetMapping("/getOrderByOrderNo/{orderNo}")
    public R<OrderInfo> getOrderByOrderNo(@PathVariable("orderNo")String orderNo){
        OrderInfo orderInfo = orderInfoService.getOne(Wrappers.lambdaQuery(OrderInfo.class)
                .eq(OrderInfo::getOrderNo, orderNo)
                .eq(OrderInfo::getOrderStatus, 0));
        return R.ok(orderInfo);
    }

    ///getOrderInfo/9
    @Operation(summary = "提交订单成功 查询订单详情")
    @GetMapping("/getOrderInfo/{id}")
    public AjaxResult getOrderInfo(@PathVariable("id")Long id){
        return success(orderInfoService.getById(id));
    }

    /*
        一个新的需求：
        1、创建接口接收请求
        2、分析请求参数如何接收
            > 请求参数：@RequestParam注解 或者pojo入参
            > 路径参数：@PathVariable
            > 请求体参数：@RequestBody
                - 提交的订单数据再请求体中通过json提交
                - 后端需要通过一个映射json结构的对象来接收数据
                    如果没有何时的类，我们可以自定义DTO(data transfer object)类接收参数
        3、编写业务
        4、分析前端需要的数据：后端业务按前端要求组装数据
            - 参考课件中的接口文档 提交订单成功返回数据库订单的id (然后页面中使用订单id查询订单信息 展示)
        5、返回数据 测试
     */
    @Operation(summary = "提交订单接口：保存订单数据")
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderSubmitDTO orderSubmitDTO){
        Long orderId = orderInfoService.submitOrder(orderSubmitDTO);
        return success(orderId);
    }
    /*
    get /order/orderInfo/trade
    响应数据 属性 和现有的实体类都不同，我们可以自定义VO类封装数据
    data: {
        "totalAmount": 8998.00,
        "orderItemList": [
            {
                "orderId": null,
                "skuId": 9,
                "skuName": "华为笔记本 32G",
                "thumbImg": "http://139.198.127.41:9000/spzx/20230525/c8f2eae0d36b6270.jpg.avif",
                "skuPrice": 5999.00,
                "skuNum": 1
            },
            ...
        ],
        "tradeNo": "1d76f36b59414e869e843fc742e21469"
    }
     */
    ///orderInfo/trade
    @Operation(summary = "去结算接口：从购物车页面点击去结算按钮")
    @GetMapping("/trade")
    public AjaxResult trade(){
        //为订单确认页 准备数据展示
       OrderTradeVO tradeVO =  orderInfoService.trade();
        return success(tradeVO);
    }

///orderInfo/submitOrder

    /**
     * 查询订单列表
     */
    @Operation(summary = "查询订单列表")
    @RequiresPermissions("order:orderInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo)
    {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Operation(summary = "导出订单列表")
    @RequiresPermissions("order:orderInfo:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo)
    {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     * 路径参数： 将这一级路径的值使用 id接收
     *
     */
    @Operation(summary = "获取订单详细信息")
    @RequiresPermissions("order:orderInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderInfoService.getById(id));
    }

    /**
     * 新增订单
     */
    @Operation(summary = "新增订单")
    @RequiresPermissions("order:orderInfo:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.save(orderInfo));
    }

    /**
     * 修改订单
     */
    @Operation(summary = "修改订单")
    @RequiresPermissions("order:orderInfo:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.updateById(orderInfo));
    }

    /**
     * 删除订单
     */
    @Operation(summary = "删除订单")
    @RequiresPermissions("order:orderInfo:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderInfoService.removeBatchByIds(Arrays.asList(ids)));
    }
}
